package com.ebt.m.customer.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ebt.m.R;
import com.ebt.m.customer.util.DisplayUtil;

public class CustomerInfoUnitTextView4Birthday extends LinearLayout {

    private static final int DEFAULT_TEXT_VIEW_TEXT_TITLE_COLOR = 0xff000000;
    private static final int DEFAULT_TEXT_VIEW_TEXT_CONTENT_COLOR = 0xff000000;
    private static final int DEFAULT_EDIT_TEXT_HINT_COLOR = 0xffbbbbbb;

    private static final int DEFAULT_TEXT_VIEW_TEXT_TITLE_SIZE_SP = 16;
    private static final int DEFAULT_TEXT_VIEW_TEXT_CONTENT_SIZE_SP = 16;

    private Context mContext;

    private String mTextViewTextTitle;
    private String mTextViewTextContent;

    private int mEditTextHintColor = DEFAULT_EDIT_TEXT_HINT_COLOR;
    private int mTextViewTextTitleColor = -1;
    private float mTextViewTextTitleSize = -1;

    private int mTextViewTextContentColor = -1;
    private float mTextViewTextContentSize = -1;

    private TextView customer_info_unit_textview_title;
    private TextView customer_info_unit_textview_content;
    private TextView customer_info_unit_textview_hint;
    private ImageView customer_info_unit_imageview;
    private ImageView iv_date_image;
    private LinearLayout birthday_ll;

    private boolean mShowImageView = false;
    private int mTitleTextViewWidth = 0;

    public CustomerInfoUnitTextView4Birthday(Context context) {
        this(context, null);
    }

    public CustomerInfoUnitTextView4Birthday(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    private String mEditTextHint;

    public CustomerInfoUnitTextView4Birthday(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        mContext = context;
        View contentView = inflate(context, R.layout.view_customer_info_unit_text_view_4_birthday, this);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.customerInfoUnit);

        final int count = a.getIndexCount();
        for (int i = 0; i < count; ++i) {
            int attr = a.getIndex(i);
            switch (attr) {
                case R.styleable.customerInfoUnit_customerInfoUnitText:
                    mTextViewTextTitle = a.getString(attr);
                    break;
                case R.styleable.customerInfoUnit_customerInfoUnitEditTextColor:
                    mTextViewTextContentColor = a.getColor(attr, DEFAULT_TEXT_VIEW_TEXT_CONTENT_COLOR);
                    break;
                case R.styleable.customerInfoUnit_customerInfoUnitEditTextSize:
                    mTextViewTextContentSize = a.getDimensionPixelSize(attr, DisplayUtil.sp2px(mContext, DEFAULT_TEXT_VIEW_TEXT_CONTENT_SIZE_SP));
                    break;
                case R.styleable.customerInfoUnit_customerInfoUnitShowImage:
                    mShowImageView = a.getBoolean(attr, false);
                    break;
                case R.styleable.customerInfoUnit_customerInfoUnitTextColor:
                    mTextViewTextTitleColor = a.getColor(attr, DEFAULT_TEXT_VIEW_TEXT_TITLE_COLOR);
                    break;
                case R.styleable.customerInfoUnit_customerInfoUnitTextSize:
                    mTextViewTextTitleSize = a.getDimensionPixelSize(attr, DisplayUtil.sp2px(mContext, DEFAULT_TEXT_VIEW_TEXT_TITLE_SIZE_SP));
                    break;
                case R.styleable.customerInfoUnit_customerInfoUnitHintColor:
                    mEditTextHintColor = a.getColor(attr, DEFAULT_EDIT_TEXT_HINT_COLOR);
                    break;
                case R.styleable.customerInfoUnit_customerInfoUnitHint:
                    mEditTextHint = a.getString(attr);
                    break;
                case R.styleable.customerInfoUnit_customerInfoUnitTitleWidth:
                    mTitleTextViewWidth = a.getDimensionPixelOffset(attr, 0);
                    break;

            }
        }
        a.recycle();

        customer_info_unit_textview_title = (TextView) contentView.findViewById(R.id.customer_info_unit_textview_title);
        customer_info_unit_textview_content = (TextView) contentView.findViewById(R.id.customer_info_unit_textview_content);
        customer_info_unit_textview_hint = (TextView) contentView.findViewById(R.id.customer_info_unit_textview_hint);
        customer_info_unit_imageview = (ImageView) contentView.findViewById(R.id.customer_info_unit_imageview);
        iv_date_image = (ImageView) contentView.findViewById(R.id.iv_date_image);
        birthday_ll = (LinearLayout) contentView.findViewById(R.id.birthday_ll);

        if (mTitleTextViewWidth > 0) {
            customer_info_unit_textview_title.setWidth(mTitleTextViewWidth);
        }
        if (mShowImageView) {
            customer_info_unit_imageview.setVisibility(View.VISIBLE);
        } else {
            customer_info_unit_imageview.setVisibility(View.GONE);
        }

        if (!TextUtils.isEmpty(mTextViewTextTitle)) {
            customer_info_unit_textview_title.setText(mTextViewTextTitle);
        }

        if (!TextUtils.isEmpty(mTextViewTextContent)) {
            customer_info_unit_textview_content.setText(mTextViewTextContent);
        }
        if (mTextViewTextContentColor >= 0) {
            customer_info_unit_textview_content.setTextColor(mTextViewTextContentColor);
        }
        if (mTextViewTextContentSize >= 0) {
            customer_info_unit_textview_content.setTextSize(DisplayUtil.px2sp(mContext, mTextViewTextContentSize));
        }
        if (mTextViewTextTitleColor >= 0) {
            customer_info_unit_textview_title.setTextColor(mTextViewTextTitleColor);
        }
        if (mTextViewTextTitleSize >= 0) {
            customer_info_unit_textview_title.setTextSize(DisplayUtil.px2sp(mContext, mTextViewTextTitleSize));
        }
        if (!TextUtils.isEmpty(mEditTextHint)) {
            customer_info_unit_textview_content.setHint(mEditTextHint);
        }
        customer_info_unit_textview_content.setHintTextColor(mEditTextHintColor);
    }

    public void setTextViewTextTitle(String text) {
        mTextViewTextTitle = text;
        if (customer_info_unit_textview_title != null) {
            customer_info_unit_textview_title.setText(mTextViewTextTitle);
        }
    }

    public void setTextViewTextContent(String text) {
        mTextViewTextContent = text;
        if (customer_info_unit_textview_content != null) {
            if (mTextViewTextContent != null
                    && mTextViewTextContent.trim().length() > 0
                    && mTextViewTextContent.trim().charAt(0) >= 48
                    && mTextViewTextContent.trim().charAt(0) <= 57) {
                customer_info_unit_textview_content.setText(mTextViewTextContent);
                setIsConfirmedUI(true);
            } else {
                setIsConfirmedUI(false);
            }
        }
    }

    public void setIsConfirmedUI(boolean isConfirmedUI){
        if(isConfirmedUI){
            iv_date_image.setVisibility(View.GONE);
            customer_info_unit_textview_hint.setVisibility(View.GONE);
            customer_info_unit_textview_content.setVisibility(View.VISIBLE);
        }else{
            iv_date_image.setVisibility(View.VISIBLE);
            customer_info_unit_textview_hint.setVisibility(View.VISIBLE);
            customer_info_unit_textview_content.setVisibility(View.GONE);
        }
    }

    public String getTextViewTextTitle() {
        return (String) customer_info_unit_textview_title.getText();
    }

    public String getTextViewTextContent() {
        return (String) customer_info_unit_textview_content.getText();
    }

    public void setTextViewContentOnClickListener(OnClickListener listener, Object tag) {
//		customer_info_unit_textview_content.setTag(tag);
//		customer_info_unit_textview_content.setOnClickListener(listener);
        birthday_ll.setTag(tag);
        birthday_ll.setOnClickListener(listener);
    }


    public TextView getTitleTextView() {
        return customer_info_unit_textview_title;
    }

    public TextView getContentTextView() {
        return customer_info_unit_textview_content;
    }
}
